package edu.guet.cn.blogbasics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.guet.cn.blogbasics.common.ResultEnum;
import edu.guet.cn.blogbasics.common.ResultVO;
import edu.guet.cn.blogbasics.entity.Worksheet;
import edu.guet.cn.blogbasics.service.WorksheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hejianding
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/worksheet")
public class WorksheetController {
    @Autowired
    WorksheetService worksheetService;

    /**
     * 添加工作表
     * @param worksheet
     * @return 结果集
     */
    @PostMapping("/addWorksheet")
    public Map<String,Object> addWorksheet(@RequestBody Worksheet worksheet){
        QueryWrapper<Worksheet> wrapper = new QueryWrapper<>();
        wrapper.eq("plan", worksheet.getPlan())
                .eq("user_name", worksheet.getUserName());
        Worksheet worksheet1 = worksheetService.getOne(wrapper);
        if(worksheet.getPlan()==null||worksheet.getPlanTime()==null){
            return ResultVO.result(ResultEnum.ADD_WORKSHEET_FAILED1, false);
        }else if(worksheet1!=null){
            return ResultVO.result(ResultEnum.ADD_WORKSHEET_FAILED2, false);
        }else {
            worksheetService.save(worksheet);
            return ResultVO.result(ResultEnum.ADD_WORKSHEET_SUCCESS, true);
        }
    }

    /**
     * 查看工作表
     * @param userName
     * @return 查看结果集
     */
    @GetMapping("/getWorksheet")
    public List<Worksheet> selectWorksheetByUserName(String userName){
        return worksheetService.selectWorksheetByUserName(userName);
    }

    /**
     * 更新工作表
     * @param worksheet
     * @return 更新结果集
     */
    @PostMapping("/updateWorkSheet")
    public Map<String,Object> updateWorksheet(@RequestBody Worksheet worksheet){
        if (worksheet!=null){
            worksheetService.updateWorksheet(worksheet);
            return ResultVO.result(ResultEnum.UPDATE_WORKSHEET_SUCCESS, true);
        }else {
            return ResultVO.result(ResultEnum.UPDATE_WORKSHEET_FAILED, false);
        }
    }

    /**
     * 删除工作日志
     * @param worksheet
     * @return 删除结果集
     */
    @PostMapping("/deleteWorksheet")
    public Map<String,Object> deleteWorksheet(@RequestBody Worksheet worksheet){
        worksheetService.deleteWorksheet(worksheet);
        QueryWrapper<Worksheet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan", worksheet.getPlan())
                    .eq("user_name",worksheet.getUserName());
        Worksheet worksheet1=worksheetService.getOne(queryWrapper);
        System.out.println(worksheet1);
        if(worksheet1==null){
            return ResultVO.result(ResultEnum.DELETE_WORKSHEET_SUCCESS,true);
        }else {
            return ResultVO.result(ResultEnum.DELETE_WORKSHEET_FAILED,false);

        }
    }
}

